// pages/forgetPwd/forgetPwd.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendText: "发送邮箱验证码",
    send: false,
    userAccount: null,
    showPwd: true,
    delPwd: false,
    img: "unshow",
    inputPwd: null
  },
  upDataPwd: function(e) { //重置密码
    console.log(e)
    var that = this
    if (e.detail.value.userAccount == "") {
      setTimeout(function() {
        wx.showModal({
          title: '提示',
          content: '请输入你的账号',
          confirmText: "好的",
          confirmColor: "#4fa0e4",
          showCancel: false
        })
      }, 0)
    } else if (e.detail.value.userEmail == "") {
      wx.showModal({
        title: '提示',
        content: '请输入你的邮箱地址',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userCode == "") {
      wx.showModal({
        title: '提示',
        content: '请输入验证码',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userCode.length < 6) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的验证码',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userPwd_new == "") {
      wx.showModal({
        title: '提示',
        content: '请输入新密码',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (e.detail.value.userPwd_new != "") {

    } else if (e.detail.value.userAccount != that.data.userAccount) {
      wx.showModal({
        title: '提示',
        content: '此用户不存在',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else {
      wx.request({
        url: 'https://www.maintenance.ink/student/user/resetInfoPost',
        header: {
          'content-type': 'application/json;charset=utf-8'
        },
        method: "POST",
        data: {
          userAccount: e.detail.value.userAccount,
          userPwd: e.detail.value.userPwd_new,
          userCode: e.detail.value.userCode
        },
        success(res) {
          setTimeout(function() {
            wx.showModal({
              title: '提示',
              content: '密码修改成功',
              confirmText: "好的",
              confirmColor: "#4fa0e4",
              showCancel: false
            })
            setTimeout(function() {
              wx.switchTab({
                url: '/pages/user/user'
              })
            }, 1500)
          }, 0)
        }
      })
    }
  },
  sendCode: function(e) { //发送验证码
    var that = this
    console.log(e)
    if (e.detail.value.userAccount == "") {
      setTimeout(function() {
        wx.showModal({
          title: '提示',
          content: '请输入你的账号',
          confirmText: "好的",
          confirmColor: "#4fa0e4",
          showCancel: false
        })
      }, 0)
    } else if (e.detail.value.userEmail == "") {
      wx.showModal({
        title: '提示',
        content: '请输入你的邮箱地址',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(e.detail.value.userEmail)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的邮箱地址',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else {
      wx.request({
        url: 'https://www.maintenance.ink/student/user/resetInfoEmailVerifyPost',
        header: {
          'content-type': 'application/json;charset=utf-8'
        },
        method: "POST",
        data: {
          userEmail: e.detail.value.userEmail,
          title: "往往天气",
          alias: "PassWord"
        },
        success(res) {
          console.log("邮箱验证码", res)
          that.setCode()
          that.setData({
            userAccount: res.data.user.userAccount
          })
        }
      })
    }
  },
  setCode() { //验证码倒计时
    var that = this
    let time = 61
    var interval = null
    interval = setInterval(function() {
      time--;
      that.setData({
        sendText: time + "秒",
        send: true
      })
      if (time <= 0) {
        clearInterval(interval)
        that.setData({
          sendText: "重新发送",
          time: 61,
          send: false
        })
      }
    }, 1000)
  },
  showPwd() {
    this.setData({
      showPwd: this.data.showPwd ? false : true,
      img: this.data.showPwd ? "show" : "unshow"
    })
  },
  inputPwd(e) {
    if (e.detail.value != "") {
      this.setData({
        delPwd: true,
        inputPwd: e.detail.value
      })
    } else {
      this.setData({
        delPwd: false
      })
    }
  },
  hasPwd(e) {
    // console.log(e)
    if (e.detail.value == "") {
      this.setData({
        delPwd: false
      })
    } else {
      this.setData({
        delPwd: true
      })
    }
  },
  noPwd(e) {
    // console.log(e)
    this.setData({
      delPwd: false
    })
  },
  delPwd() {
    console.log(this.data.inputPwd)
    this.setData({
      inputPwd: null
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#4fa0e4',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})